﻿using Microsoft.Extensions.DependencyInjection;
using SampleAvaloniaMvvm.ViewModels;
using SampleAvaloniaMvvm.Views;
using System;
namespace SampleAvaloniaMvvm
{
    internal class ServicesLocator
    {
        static IServiceProvider provider;
        // IServiceCollection collection;
        public ServicesLocator()
        {
            if (provider != null) {
                return;
            }
            provider = new ServiceCollection().AddSingleton<MainWindowViewModel>()
                .AddSingleton<IServiceProvider>(p => provider)
                .AddSingleton<MainWindow>((p) => {
                    return new MainWindow() {
                        DataContext = p.GetService<MainWindowViewModel>(),
                    };
                }).BuildServiceProvider();
        }
        public MainWindowViewModel MainWindowViewModel
        {
            get => provider.GetService<MainWindowViewModel>();
        }
        public MainWindow MainWindow
        {
            get => provider.GetService<MainWindow>();
        }
    }
}
